package com.xl.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.generator.common.ActionResult;
import com.xl.generator.common.ActionResultService;
import com.xl.generator.domain.SubjectiveQuestion;
import com.xl.generator.mapper.SubjectiveQuestionMapper;
import com.xl.generator.pojo.PageList;
import com.xl.generator.service.SubjectiveQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈〉<br>
 *
 * @author lvln5
 * @Description
 * @date 2022/11/12 19:39
 */
@Api(value = "主观题的增删改查")
@RestController
@RequestMapping("subjectiveQuestion")
public class SubjecttveQuestionController {

	@Resource
	private SubjectiveQuestionService subjectiveQuestionService;
	@Resource
	private SubjectiveQuestionMapper subjectiveQuestionMapper;

	@ApiOperation(value = "分页查询所有题目", notes="")
	@PostMapping ("list")
	public ActionResult test(@RequestParam("current") Integer current,@RequestParam("Size") Integer size){
		IPage<SubjectiveQuestion> choiceQuestion = new Page<>();
		choiceQuestion.setCurrent(current);
		choiceQuestion.setSize(size);
		IPage<SubjectiveQuestion> page = subjectiveQuestionService.page(choiceQuestion);
		PageList pageList = new PageList();
		pageList.setData(page);
		pageList.setTotal(pageList.getTotal());
		return ActionResultService.callBackResultOK(pageList);
	}
	@ApiOperation(value = "随机生成主观题目，通过这个来区分QuestionType", notes="通过QuestionType来确认题目，这里主要程序猿进阶赛3，和终极赛题目设置4")
	@PostMapping ("getQuestion")
	public ActionResult question(@RequestBody SubjectiveQuestion choiceQuestion){
		LambdaQueryWrapper<SubjectiveQuestion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SubjectiveQuestion::getQuestionType,choiceQuestion.getQuestionType());
		if (choiceQuestion.getQuestionType()==4){
			queryWrapper.last("ORDER BY RAND() limit 2");
		}else {
			queryWrapper.last("ORDER BY RAND() limit 2");
		}
		List<SubjectiveQuestion> list = subjectiveQuestionMapper.selectList(queryWrapper);
		return ActionResultService.callBackResultOK(list);
	}

	@ApiOperation(value = "每日一题", notes="每日一题")
	@PostMapping ("oneDayQuestion")
	public ActionResult oneDayQuestion(@RequestBody SubjectiveQuestion choiceQuestion){
		LambdaQueryWrapper<SubjectiveQuestion> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.eq(SubjectiveQuestion::getQuestionType,choiceQuestion.getQuestionType());
		queryWrapper.last("ORDER BY RAND() limit 1");
		List<SubjectiveQuestion> list = subjectiveQuestionMapper.selectList(queryWrapper);
		return ActionResultService.callBackResultOK(list);
	}

	@ApiOperation(value = "主观题难度查题分页", notes="难度查题questionType 3 4 ")
	@PostMapping ("get")
	public ActionResult get(@RequestBody SubjectiveQuestion subjectiveQuestion,@RequestParam("current") Integer current,@RequestParam("Size") Integer size){
		IPage<SubjectiveQuestion> choiceQuestion1 = new Page<>();
		choiceQuestion1.setCurrent(current);
		choiceQuestion1.setSize(size);
		LambdaQueryWrapper<SubjectiveQuestion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(SubjectiveQuestion::getQuestionType,subjectiveQuestion.getQuestionType());
		IPage<SubjectiveQuestion> list = subjectiveQuestionService.page(choiceQuestion1, lambdaQueryWrapper);
		return ActionResultService.callBackResultOK(list);
	}
	@ApiOperation(value = "主观题搜索问题名称 查题分页", notes="搜索名称查题，支持模糊查询")
	@PostMapping ("search")
	public ActionResult search(@RequestBody SubjectiveQuestion subjectiveQuestion,@RequestParam("current") Integer current,
			@RequestParam("Size") Integer size){
		IPage<SubjectiveQuestion> ch = new Page<>();
		ch.setCurrent(current);
		ch.setSize(size);
		LambdaQueryWrapper<SubjectiveQuestion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.like(subjectiveQuestion.getQuestion()!=null,SubjectiveQuestion::getQuestion,subjectiveQuestion.getQuestion())
		.eq(subjectiveQuestion.getQuestionType() != null,SubjectiveQuestion::getQuestionType,subjectiveQuestion.getQuestionType());
		IPage<SubjectiveQuestion> list  = subjectiveQuestionService.page(ch, lambdaQueryWrapper);
		return ActionResultService.callBackResultOK(list);
	}
	/**
	 * @param
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "添加题目", notes="注册成功状态码为o,否侧为-1失败")
	@PostMapping("add")
	public ActionResult register(@RequestBody SubjectiveQuestion subjectiveQuestion){
		boolean save = subjectiveQuestionService.save(subjectiveQuestion);
		if (save){
			return ActionResultService.callBackResultOK("添加成功");
		}
		return ActionResultService.callBackResultOK("添加失败");
	}
	/**
	 * @param
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "删除题目", notes="删除成功状态码为o,否侧为-1失败")
	@PostMapping("del")
	public ActionResult del(@RequestBody SubjectiveQuestion subjectiveQuestion){
		LambdaQueryWrapper<SubjectiveQuestion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SubjectiveQuestion::getId,subjectiveQuestion.getId());
		boolean remove = subjectiveQuestionService.remove(queryWrapper);
		if (remove){
			return	ActionResultService.callBackResultOK("删除成功");
		}
		return ActionResultService.callBackResultFail("删除失败");
	}
	@ApiOperation(value = "更新题目", notes="更新成功状态码为o,否侧为-1失败")
	@PostMapping("update")
	public ActionResult updata(@RequestBody SubjectiveQuestion subjectiveQuestion){
		boolean update = subjectiveQuestionService.updateById(subjectiveQuestion);
		if (update){
			return	ActionResultService.callBackResultOK("更新成功");
		}
		return ActionResultService.callBackResultFail("更新失败");
	}
}
